﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCE.UI.Blocks;
using Baosight.ZNH.ZMCE.UI.PopWindows;

namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCEU0 : EFViewCmes
    {
        private UnusualProblemBlock block = new UnusualProblemBlock();
        private WorkPlanBlock blockPlan = new WorkPlanBlock();
        private NoteBlock blockNote = new NoteBlock();
        private string actionType;

        public ZMCEU0()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            //initData.AddBlock(block.getHandleStatus("cmes.ce.handleStatus", true));
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "异常问题-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            actionType = "update";
            base.OnUpdateButtonClickPopup(eiInfo, "异常问题-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 生成计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnGeneratePlanButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录生成计划！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录生成计划！", true);
                return;
            }
            else
            {
                if ("未处理".Equals(this.ViewProperty.EFGridCheckedRows[0]["fdStatus"].ToString()))
                {
                    EIInfo eiInfo = new EIInfo();
                    EIBlock eiBlock = new EIBlock("result");

                    EIColumn column = new EIColumn();
                    column.SetName("unusualId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("workrecordId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("flag");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdUseDepartmentId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    EIRow row = new EIRow();
                    //row.Add("unusualId", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    row.Add("workrecordId", this.grid.GetCheckedEIRows()[0]["fdWorkrecordId"]);
                    //row.Add("flag", "UnusualProblem");
                    row.Add("fdUseDepartmentId", this.grid.GetCheckedEIRows()[0]["fdUseDepartmentId"]);

                    eiBlock.AddRow(row);
                    eiInfo.AddBlock(eiBlock);
                    eiInfo.Set("fdEquipmentId", this.grid.GetCheckedEIRows()[0]["fdEquipmentId"]);
                    eiInfo.Set("fdEquipmentName", this.grid.GetCheckedEIRows()[0]["fdEquipmentName"]);
                    eiInfo.Set("fdEquipmentCode", this.grid.GetCheckedEIRows()[0]["fdEquipmentCode"]);
                    eiInfo.Set("fdUseDepartment", this.grid.GetCheckedEIRows()[0]["fdUseDepartment"]);
                    eiInfo.Set("fdSourceId", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    eiInfo.Set("fdSource", "异常");

                    OpenPlanPopup(eiInfo, "异常问题-生成计划", "insert", null);
                }
                else
                {
                    base.OpenEFWindowAlert("只能对状态为“未处理”的记录生成计划！", true);
                    return;
                }
            }
        }

        /// <summary>
        /// 生成计划弹出窗口
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public void OpenPlanPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            WorkPlan workPlan = new WorkPlan(_dialogType);//构造一个新的修改页面
            workPlan.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(blockPlan.getPlanType("cmes.ce.planType", false));
            _eIInfo.AddBlock(blockPlan.getPlanStatus("cmes.ce.planStatus", false));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.departmentWithSpace"));

            (workPlan.fdEquipmentId.Content as CmesCustomView).Value = _eIInfo.Get("fdEquipmentId").ToString();
            (workPlan.fdEquipmentId.Content as CmesCustomView).Display = _eIInfo.Get("fdEquipmentName").ToString();
            workPlan.fdEquipmentCode.Value = _eIInfo.Get("fdEquipmentCode").ToString();
            workPlan.fdUseDepartment.Value = _eIInfo.Get("fdUseDepartment").ToString();
            workPlan.fdSourceId.Value = _eIInfo.Get("fdSourceId").ToString();
            workPlan.fdSource.Value = _eIInfo.Get("fdSource").ToString();

            workPlan.fdStatus.Value = "计划";
            workPlan.fdEquipmentId.IsEnabled = false;
            workPlan.fdStatus.IsEnabled = false;
            workPlan.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = workPlan;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口

        }

        /// <summary>
        /// 给指定的oldBlock添加一个空行，形成一个新的blockName
        /// </summary>
        /// <param name="oldBlock"></param>
        /// <param name="blockName"></param>
        /// <returns></returns>
        //private EIBlock ProcessBlock(EIBlock oldBlock, string blockName)
        //{
        //    EIBlock eiBlock = new EIBlock(blockName);
        //    eiBlock.SetColumns(oldBlock.GetColumns());

        //    IList<EIRow> rows = oldBlock.GetRows();
        //    EIRow _row = new EIRow();
        //    _row.Add("orgCode", "");
        //    _row.Add("orgName", "");
        //    rows.Insert(0, _row);

        //    eiBlock.AddRows(rows);
        //    return eiBlock;
        //}

        /// <summary>
        /// 生成工单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnGenerateNoteButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录生成工单！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录生成工单！", true);
                return;
            }
            else
            {
                if ("未处理".Equals(this.ViewProperty.EFGridCheckedRows[0]["fdStatus"].ToString()))
                {
                    EIInfo eiInfo = new EIInfo();
                    EIBlock eiBlock = new EIBlock("result");

                    EIColumn column = new EIColumn();
                    column.SetName("unusualId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("workrecordId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("flag");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    column = new EIColumn();
                    column.SetName("fdUseDepartmentId");
                    column.SetTypeLabel("C");
                    eiBlock.AddColumn(column);

                    EIRow row = new EIRow();
                    //row.Add("unusualId", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    row.Add("workrecordId", this.grid.GetCheckedEIRows()[0]["fdWorkrecordId"]);
                    //row.Add("flag", "UnusualProblem");
                    row.Add("fdUseDepartmentId", this.grid.GetCheckedEIRows()[0]["fdUseDepartmentId"]);

                    eiBlock.AddRow(row);
                    eiInfo.AddBlock(eiBlock);
                    eiInfo.Set("fdEquipmentId", this.grid.GetCheckedEIRows()[0]["fdEquipmentId"]);
                    eiInfo.Set("fdEquipmentName", this.grid.GetCheckedEIRows()[0]["fdEquipmentName"]);
                    eiInfo.Set("fdEquipmentCode", this.grid.GetCheckedEIRows()[0]["fdEquipmentCode"]);
                    eiInfo.Set("fdUseDepartment", this.grid.GetCheckedEIRows()[0]["fdUseDepartment"]);
                    eiInfo.Set("fdPlanId", this.grid.GetCheckedEIRows()[0]["fdKeyId"]);
                    eiInfo.Set("fdSource", "异常");

                    OpenNotePopup(eiInfo, "异常问题-生成工单", "insert", null);
                }
                else
                {
                    base.OpenEFWindowAlert("只能对状态为“未处理”的记录生成工单！", true);
                    return;
                }
            }
        }

        /// <summary>
        /// 生成无计划工单弹出窗口
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public void OpenNotePopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            NoteManagementNoPlan note = new NoteManagementNoPlan(_dialogType);//构造一个新的修改页面
            note.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(blockNote.getNoteType("cmes.ce.noteType", false));
            _eIInfo.AddBlock(blockNote.getNoteStatus("cmes.ce.noteStatus", false));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.people"));

            (note.fdEquipmentId.Content as CmesCustomView).Value = _eIInfo.Get("fdEquipmentId").ToString();
            (note.fdEquipmentId.Content as CmesCustomView).Display = _eIInfo.Get("fdEquipmentName").ToString();
            note.fdStatus.Value = "执行";
            note.fdEquipmentCode.Value = _eIInfo.Get("fdEquipmentCode").ToString();
            note.fdDepartment.Value = _eIInfo.Get("fdUseDepartment").ToString();
            note.fdPlanId.Value = _eIInfo.Get("fdPlanId").ToString();
            note.fdSource.Value = _eIInfo.Get("fdSource").ToString();
            note.fdEquipmentId.IsEnabled = false;
            note.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = note;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口

        }

        /// <summary>
        /// 关闭问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnCloseProblemButtonClick(object sender, RoutedEventArgs e)
        {
            actionType = "closeProblem";
            base.OnModifyButtonClick("closeProblem", "关闭", sender, e);
        }

        /// <summary>
        /// 删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            actionType = "delete";
            //int count = this.grid.CheckedEIRows.Count;
            //if (count <= 0)
            //{
            //    base.OpenEFWindowAlert("请选择需要删除的记录！", true);
            //    return;
            //}
            //else
            //{
            //    for (int i = 0; i < count; i++)
            //    {
            //        if (!"草拟".Equals(this.grid.CheckedEIRows[i]["fdStatus"].ToString()))
            //        {
            //            base.OpenEFWindowAlert("只能删除状态为草拟的记录！", true);
            //            return;
            //        }
            //    }
            //}

            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 生效按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPublishButtonClick(object sender, RoutedEventArgs e)
        {
            actionType = "publish";
            //int count = this.grid.CheckedEIRows.Count;
            //if (count <= 0)
            //{
            //    base.OpenEFWindowAlert("请选择需要生效的记录！", true);
            //    return;
            //}
            //else
            //{
            //    for (int i = 0; i < count; i++)
            //    {
            //        if (!"草拟".Equals(this.grid.CheckedEIRows[i]["fdStatus"].ToString()))
            //        {
            //            base.OpenEFWindowAlert("只能生效状态为草拟的记录！", true);
            //            return;
            //        }
            //    }
            //}

            base.OnModifyButtonClick("publish", "生效", sender, e);
        }



        /// <summary>
        /// 查看明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "异常问题明细-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            UnusualProblem window = new UnusualProblem(_dialogType);//构造一个新的修改页面
            window.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(block.getQuestionSource("cmes.ce.questionSource", false));
            _eIInfo.AddBlock(block.getDegree("cmes.ce.degree", false));
            _eIInfo.AddBlock(block.getHandleStatus("cmes.ce.handleStatus", false));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.people"));

            if ("insert".Equals(_dialogType))
            {
                window.fdPerson.Value = this.EIDataContext.GetAttribute("person");
                window.fdDatetime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                window.fdStatus.Value = "草拟";
            }

            window.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = window;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 删除,修改前的判断
        /// </summary>
        /// <param name="rowList"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            bool Flag = true;
            foreach (var row in _eIRows)
            {
                if (!"草拟".Equals(row["fdStatus"].ToString()))
                {
                    if ("update".Equals(this.actionType))
                    {
                        base.OpenEFWindowAlert("只能对状态为“草拟”的记录进行修改！", true);
                        Flag = false;
                        break;
                    }
                    else if ("delete".Equals(this.actionType))
                    {
                        base.OpenEFWindowAlert("只能对状态为“草拟”的记录进行删除！", true);
                        Flag = false;
                        break;
                    }
                    else if ("publish".Equals(this.actionType))
                    {
                        base.OpenEFWindowAlert("只能生效状态为“草拟”的记录！", true);
                        Flag = false;
                        break;
                    }
                }

                if("closeProblem".Equals(this.actionType))
                {
                    if("关闭".Equals(row["fdStatus"].ToString()))
                    {
                        base.OpenEFWindowAlert("该记录已经关闭！", true);
                        Flag = false;
                        break;
                    }else if(!"处理中".Equals(row["fdStatus"].ToString()) && !"未处理".Equals(row["fdStatus"].ToString()))
                    {
                         base.OpenEFWindowAlert("只能关闭状态为处理中和未处理的记录！", true);
                         Flag = false;
                         break;
                    }
                }
               
            }
           
            return Flag;
        }
    }
}
